// 解析题目
export function parseQuestion(messages, categoryId) {
  if (!messages) {
    return
  }
  const questionMessages = messages.split(/\n+/)
  const questions = []
  let options = []
  let question = {}
  questionMessages.forEach((line) => {
    //console.log(line)
    // 题目类型
    if (line.startsWith('【单选题】')) {
      question.questionType = 1
    }
    if (line.startsWith('【多选题】')) {
      question.questionType = 2
    }
    if (line.startsWith('【判断题】')) {
      question.questionType = 3
    }
    if (line.startsWith('【填空题】')) {
      question.questionType = 4
    }
    if (line.startsWith('【简答题】')) {
      question.questionType = 5
    }

    // 题目题干
    if (line.startsWith('【')) {
      const questionTitleArray = /^\d+[.、：:](.+)/.exec(line.split('题】')[1])
      if (questionTitleArray && questionTitleArray.length > 1) {
        question.questionTitle = questionTitleArray[1]
      }
    }

    // 当题干单独占一行的时候（兼容处理）
    const questionTitleArray = /^\d+[.、：:](.+)/.exec(line)
    if (questionTitleArray && questionTitleArray.length > 1) {
      if (!question.questionTitle) {
        question.questionTitle = questionTitleArray[1]
      }
    }

    // 选项
    const optionArray = /^([A-Z])[.、：:](.+)/.exec(line)
    if (optionArray && optionArray.length > 2) {
      options.push({ key: optionArray[1], optionContent: optionArray[2] })
    }

    // 答案
    if (line.startsWith('答案')) {
      const questionAnswerArray = /^答案[：:.、](.+)/.exec(line)
      if (questionAnswerArray && questionAnswerArray.length > 1) {
        question.questionAnswer = handleQuestionAnswer(options, questionAnswerArray[1])
        if (question.questionType === 2) {
          // 多选题
          question.questionAnswer2 = question.questionAnswer.split('|')
        }
        if (question.questionType === 3) {
          // 判断题
          question.questionAnswer = question.questionAnswer.toLowerCase() === '正确' ? '对' : '错'
          options.push({ key: 'A', optionContent: '对' }, { key: 'B', optionContent: '错' })
        }
        if (question.questionType === 4) {
          // 填空题
          question.questionAnswer4 = question.questionAnswer.split('|')
        }
      }
    }

    // 难度
    if (line.startsWith('难度')) {
      const difficultyLevelArray = /^难度[：:.、](.+)/.exec(line)
      if (difficultyLevelArray && difficultyLevelArray.length > 1) {
        // 简单=1，一般=2，困难=3
        switch (difficultyLevelArray[1]) {
          case '简单':
            question.difficultyLevel = 1
            break
          case '一般':
            question.difficultyLevel = 2
            break
          case '困难':
            question.difficultyLevel = 3
            break
          default:
            question.difficultyLevel = 2
            break
        }
      }
    }

    // 解析
    if (line.startsWith('解析')) {
      const questionAnalysisArray = /^解析[：:.、](.+)/.exec(line)
      if (questionAnalysisArray && questionAnalysisArray.length > 1) {
        question.questionAnalysis = questionAnalysisArray[1]
      }
      question.categoryId = categoryId
      question.options = options
      questions.push(question)
      question = {}
      options = []
    }
  })
  return questions
}

// 处理答案
function handleQuestionAnswer(optionArray, answerContent) {
  if (optionArray && optionArray.length > 1) {
    // 从字符串中获取字母
    answerContent = answerContent.trim().replace(/[^a-zA-Z]/g, '')
    if (answerContent.length === 1) {
      // 单选
      optionArray.forEach((option) => {
        if (answerContent.toLowerCase() === option.key.toLowerCase()) {
          answerContent = option.optionContent
        }
      })
    } else {
      // 多选
      const answerOptionArray = answerContent.split('')
      console.log(answerOptionArray)
      let answerOptionContent = ''
      answerOptionArray.forEach((option) => {
        optionArray.forEach((item) => {
          if (option.toLowerCase() === item.key.toLowerCase()) {
            answerOptionContent = answerOptionContent + item.optionContent + '|'
          }
        })
      })
      answerContent = answerOptionContent.substring(0, answerOptionContent.length - 1)
      console.log(answerContent)
    }
  }
  return answerContent
}
